package com.party.controller.app;


import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.party.common.base.ApiResult;
import com.party.common.entity.MyPage;
import com.party.common.utils.PageUtil;
import com.party.entity.*;
import com.party.entity.dto.QueryResource1Dto;
import com.party.entity.dto.QueryResourceDto;
import com.party.entity.vo.AudioDetailVo;
import com.party.entity.vo.BookVo;
import com.party.service.PbDeviceService;
import com.party.service.PbResourceDeviceService;
import com.party.service.PbResourceService;

import com.party.service.impl.PbResourceServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

import static com.party.service.impl.PbResourceServiceImpl.resourceType;

/**
 * @Description: 首页产品
 * @Author:mengyangguang
 * @CreateDate:2022/9/27
 **/
@Slf4j
@RestController
@RequestMapping("/product")
@Api(tags = "只针对大屏展示的资源接口", value = "只针对大屏展示的资源接口")
public class ProductController {
    @Resource
    private PbResourceService pbResourceService;
    @Resource
    private PbDeviceService pbDeviceService;
    @Resource
    private PbResourceDeviceService pbResourceDeviceService;

    @ApiOperation("获取图书推荐列表")
    @GetMapping("/getRecommendBookList")
    public ApiResult<List<BookVo>> getRecommendBookList(@RequestParam(value = "stationCode", required = true) String stationCode) {
        LambdaQueryWrapper<PbDevice> query1 = new LambdaQueryWrapper<>();
        query1.eq(PbDevice::getDeviceCode, stationCode);
        PbDevice one = pbDeviceService.getOne(query1);
        Long id = one.getId();
        LambdaQueryWrapper<PbResourceDevice> query2 = new LambdaQueryWrapper<>();
        query2.eq(PbResourceDevice::getDeviceId, id);
        List<PbResourceDevice> list1 = pbResourceDeviceService.list(query2);
        List<BookVo> bookVoList = new ArrayList<>();
        if (list1 != null) {
            for (int i = 0; i < list1.size(); i++) {
                LambdaQueryWrapper<PbResource> query = new LambdaQueryWrapper<>();
                query.eq(PbResource::getSfRecommend, 1);
                query.eq(PbResource::getDataTypeId, 1719628518976913409L);
                query.eq(PbResource::getResourceId, list1.get(i).getResourceId());
                query.orderByDesc(PbResource::getLikeCount);
                if (pbResourceService.getOne(query) != null) {
                    PbResource one1 = pbResourceService.getOne(query);
                    BookVo bookVo = new BookVo();
                    bookVo.setAuthor(one1.getAuthor());
                    bookVo.setBookCover(one1.getCover());
                    bookVo.setBookIntroduction(one1.getBriefIntroduction());
                    bookVo.setBookName(one1.getResourceName());
                    bookVo.setId(one1.getId());
                    bookVoList.add(bookVo);
                }
            }
        }
        return ApiResult.ok1(bookVoList);
    }

    @ApiOperation("获取普通图书列表")
    @GetMapping("/getBookList")
    public ApiResult<List<BookVo>> getBookList(@RequestParam(value = "stationCode", required = true) String stationCode) {
        LambdaQueryWrapper<PbDevice> query1 = new LambdaQueryWrapper<>();
        query1.eq(PbDevice::getDeviceCode, stationCode);
        PbDevice one = pbDeviceService.getOne(query1);
        Long id = one.getId();
        LambdaQueryWrapper<PbResourceDevice> query2 = new LambdaQueryWrapper<>();
        query2.eq(PbResourceDevice::getDeviceId, id);
        query2.eq(PbResourceDevice::getSfRecommend,1);
        List<PbResourceDevice> list1 = pbResourceDeviceService.list(query2);
        List<BookVo> bookVoList = new ArrayList<>();
        if (list1 != null) {
            for (int i = 0; i < list1.size(); i++) {
                LambdaQueryWrapper<PbResource> query = new LambdaQueryWrapper<>();
                query.eq(PbResource::getDataTypeId, 1719628518976913409L);
                query.eq(PbResource::getResourceId, list1.get(i).getResourceId());
                query.orderByDesc(PbResource::getLikeCount);
                if (pbResourceService.getOne(query) != null) {
                    PbResource one1 = pbResourceService.getOne(query);
                    BookVo bookVo = new BookVo();
                    bookVo.setAuthor(one1.getAuthor());
                    bookVo.setBookCover(one1.getCover());
                    bookVo.setBookIntroduction(one1.getBriefIntroduction());
                    bookVo.setBookName(one1.getResourceName());
                    bookVo.setId(one1.getId());
                    bookVoList.add(bookVo);
                }
            }
        }
        return ApiResult.ok1(bookVoList);
    }

    @PostMapping("/queryPageBookList")
    @ApiOperation("分页获取获取图书列表")
    public ApiResult<MyPage<BookVo>> queryPage(@RequestBody QueryResource1Dto dto) {
        LambdaQueryWrapper<PbDevice> query1 = new LambdaQueryWrapper<>();
        query1.eq(PbDevice::getDeviceCode, dto.getStationCode());
        PbDevice one = pbDeviceService.getOne(query1);
        Long id = one.getId();
        LambdaQueryWrapper<PbResourceDevice> query2 = new LambdaQueryWrapper<>();
        query2.eq(PbResourceDevice::getDeviceId, id);
        List<PbResourceDevice> list1 = pbResourceDeviceService.list(query2);
        List<Long>resourceList=new ArrayList<>();
        for (int i = 0; i < list1.size(); i++) {
            resourceList.add(list1.get(i).getResourceId());
        }
        LambdaQueryWrapper<PbResource> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PbResource::getDataTypeId, 1719628518976913409L);
        wrapper.in(PbResource::getResourceId,resourceList);
        int count = pbResourceService.count(wrapper);
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<PbResource> list = pbResourceService.list(wrapper);
        List<BookVo> bookVoList = new ArrayList<>();
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                BookVo bookVo = new BookVo();
                bookVo.setAuthor(list.get(i).getAuthor());
                bookVo.setBookCover(list.get(i).getCover());
                bookVo.setBookIntroduction(list.get(i).getBriefIntroduction());
                bookVo.setBookName(list.get(i).getResourceName());
                bookVo.setId(list.get(i).getId());
                bookVoList.add(bookVo);
            }
        }
        PageInfo<BookVo> info = new PageInfo<>(bookVoList);
        MyPage<BookVo> bookVoMyPage = PageUtil.PageInfo2MyPage(info);
        bookVoMyPage.setTotal(count);
        return ApiResult.ok1(bookVoMyPage);
    }

    @ApiOperation("获取图书详细信息")
    @GetMapping("/getBookDetail")
    public ApiResult<BookVo> getBookDetail(@RequestParam(value = "id", required = true) String id) {
        PbResource byId1 = pbResourceService.getById(id);
        BookVo bookVo = new BookVo();
        bookVo.setId(byId1.getResourceId());
        bookVo.setBookName(byId1.getResourceName());
        bookVo.setBookIntroduction(byId1.getBriefIntroduction());
        bookVo.setBookCover(byId1.getCover());
        bookVo.setAuthor(byId1.getAuthor());
        bookVo.setBookCode("https://dangjian.shuzinongyue.com:8946/book?id=" + byId1.getId());
        String token = PbResourceServiceImpl.selectToken("http://10.100.24.103:9999" + "/auth/oauth2/token");
        ObjectMapper objectMapper = new ObjectMapper();
        BookExposureEntity bookExposureEntity = new BookExposureEntity();
        bookExposureEntity.setUsername("hndzyx");
        bookExposureEntity.setPassword("123456");
        bookExposureEntity.setFromSys("1");
        bookExposureEntity.setProductId("1850710022997258241");
        bookExposureEntity.setResourceId(byId1.getResourceId() + "");
        String json = null;
        try {
            json = objectMapper.writeValueAsString(bookExposureEntity);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        String jsonString =
                //"{\"code\":0,\"msg\":\"汉魏士人的思想历程详情\",\"data\":{\"id\":\"1724497002389471233\",\"bookName\":\"汉魏士人的思想历程\",\"author\":\"孙生 白笑天\",\"bookNumber\":\"978-7-227-04912-8\",\"briefIntroduction\":null,\"press\":\"宁夏人民出版社\",\"publicationTime\":\"2011-12-01\",\"bookClassification\":\"社会科学总论\",\"dataTypeId\":\"1719628518976913409\",\"bookDataType\":\"电子书\",\"bookGradeType\":\"B\",\"labelType\":null,\"copyrightOwner\":\"北京九州文心\",\"file\":[{\"fileName\":\"汉魏士人的思想历程\",\"size\":3.0,\"resourceName\":\"汉魏士人的思想历程\",\"fileUrl\":\"https://zhongtai.zhongjiaoyuntu.com:9002/dzyx/20240428/黄老师上传1宁夏吉林/汉魏士人的思想历程/汉魏士人的思想历程.pdf\",\"type\":\"pdf\"}],\"coverPictures\":[{\"coverPictureName\":\"汉魏士人的思想历程\",\"coverPictureUrl\":\"https://zhongtai.zhongjiaoyuntu.com:9002/dzyx/20240428/黄老师上传1宁夏吉林/汉魏士人的思想历程/汉魏士人的思想历程.jpg\",\"type\":\"jpg\"}]}}\n";
                resourceType(token, "http://10.100.24.103:9999" + "/digitallibrary/bookExposure/getProductResourcesDetails", json);
        log.info("资源返回报文=============={}", jsonString);

        JSONObject jsonObject = JSON.parseObject(jsonString);
        JSONArray jsonArray = jsonObject.getJSONObject("data").getJSONArray("file");
        String fileUrl = jsonArray.getJSONObject(0).getString("fileUrl");
        bookVo.setBookUrl(fileUrl);
        return ApiResult.ok1(bookVo);
    }

    @ApiOperation("获取推荐音频列表")
    @GetMapping("/getRecommendAutoList")
    public ApiResult<List<BookVo>> getRecommendAutoList(@RequestParam(value = "stationCode", required = true) String stationCode) {
        LambdaQueryWrapper<PbDevice> query1 = new LambdaQueryWrapper<>();
        query1.eq(PbDevice::getDeviceCode, stationCode);
        PbDevice one = pbDeviceService.getOne(query1);
        Long id = one.getId();
        LambdaQueryWrapper<PbResourceDevice> query2 = new LambdaQueryWrapper<>();
        query2.eq(PbResourceDevice::getDeviceId, id);
        List<PbResourceDevice> list1 = pbResourceDeviceService.list(query2);
        List<BookVo> bookVoList = new ArrayList<>();
        if (list1 != null) {
            for (int i = 0; i < list1.size(); i++) {
                LambdaQueryWrapper<PbResource> query = new LambdaQueryWrapper<>();
                query.eq(PbResource::getSfRecommend, 1);
                query.eq(PbResource::getDataTypeId, 1719628549662441474L);
                query.eq(PbResource::getResourceId, list1.get(i).getResourceId());
                query.orderByDesc(PbResource::getLikeCount);
                if (pbResourceService.getOne(query) != null) {
                    PbResource one1 = pbResourceService.getOne(query);
                    BookVo bookVo = new BookVo();
                    bookVo.setAuthor(one1.getAuthor());
                    bookVo.setBookCover(one1.getCover());
                    bookVo.setBookIntroduction(one1.getBriefIntroduction());
                    bookVo.setBookName(one1.getResourceName());
                    bookVo.setId(one1.getId());
                    bookVoList.add(bookVo);
                }
            }
        }
        return ApiResult.ok1(bookVoList);
    }

    @ApiOperation("获取普通音频列表")
    @GetMapping("/getAutoList")
    public ApiResult<List<BookVo>> getAutoList(@RequestParam(value = "stationCode", required = true) String stationCode) {
        LambdaQueryWrapper<PbDevice> query1 = new LambdaQueryWrapper<>();
        query1.eq(PbDevice::getDeviceCode, stationCode);
        PbDevice one = pbDeviceService.getOne(query1);
        Long id = one.getId();
        LambdaQueryWrapper<PbResourceDevice> query2 = new LambdaQueryWrapper<>();
        query2.eq(PbResourceDevice::getDeviceId, id);
        query2.eq(PbResourceDevice::getSfRecommend,1);
        List<PbResourceDevice> list1 = pbResourceDeviceService.list(query2);
        List<BookVo> bookVoList = new ArrayList<>();
        if (list1 != null) {
            for (int i = 0; i < list1.size(); i++) {
                LambdaQueryWrapper<PbResource> query = new LambdaQueryWrapper<>();
                query.eq(PbResource::getDataTypeId, 1719628549662441474L);
                query.eq(PbResource::getResourceId, list1.get(i).getResourceId());
                query.orderByDesc(PbResource::getLikeCount);
                if (pbResourceService.getOne(query) != null) {
                    PbResource one1 = pbResourceService.getOne(query);
                    BookVo bookVo = new BookVo();
                    bookVo.setAuthor(one1.getAuthor());
                    bookVo.setBookCover(one1.getCover());
                    bookVo.setBookIntroduction(one1.getBriefIntroduction());
                    bookVo.setBookName(one1.getResourceName());
                    bookVo.setId(one1.getId());
                    bookVoList.add(bookVo);
                }
            }
        }
        return ApiResult.ok1(bookVoList);
    }

    @PostMapping("/queryPageAutoList")
    @ApiOperation("分页获取获取音频列表")
    public ApiResult<MyPage<BookVo>> queryPageAutoList(@RequestBody QueryResource1Dto dto) {
        LambdaQueryWrapper<PbDevice> query1 = new LambdaQueryWrapper<>();
        query1.eq(PbDevice::getDeviceCode, dto.getStationCode());
        PbDevice one = pbDeviceService.getOne(query1);
        Long id = one.getId();
        LambdaQueryWrapper<PbResourceDevice> query2 = new LambdaQueryWrapper<>();
        query2.eq(PbResourceDevice::getDeviceId, id);
        List<PbResourceDevice> list1 = pbResourceDeviceService.list(query2);
        List<Long>resourceList=new ArrayList<>();
        for (int i = 0; i < list1.size(); i++) {
            resourceList.add(list1.get(i).getResourceId());
        }
        LambdaQueryWrapper<PbResource> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PbResource::getDataTypeId, 1719628549662441474L);
        wrapper.in(PbResource::getResourceId,resourceList);
        wrapper.orderByDesc(PbResource::getLikeCount);
        int count = pbResourceService.count(wrapper);
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<PbResource> list = pbResourceService.list(wrapper);
        List<BookVo> bookVoList = new ArrayList<>();
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                BookVo bookVo = new BookVo();
                bookVo.setAuthor(list.get(i).getAuthor());
                bookVo.setBookCover(list.get(i).getCover());
                bookVo.setBookIntroduction(list.get(i).getBriefIntroduction());
                bookVo.setBookName(list.get(i).getResourceName());
                bookVo.setId(list.get(i).getId());
                bookVoList.add(bookVo);
            }
        }
        PageInfo<BookVo> info = new PageInfo<>(bookVoList);
        MyPage<BookVo> bookVoMyPage = PageUtil.PageInfo2MyPage(info);
        bookVoMyPage.setTotal(count);
        return ApiResult.ok1(bookVoMyPage);
    }

    @ApiOperation("获取音频详情信息")
    @GetMapping("/getAudioDetail")
    public AudioDetailVo getAudioDetail(@RequestParam(value = "id", required = true) String id) {
        PbResource byId1 = pbResourceService.getById(id);
//        bookVo.setBookCode("https://dangjian.shuzinongyue.com:8946/book?id=" + byId1.getResourceId());
        String token = PbResourceServiceImpl.selectToken("http://10.100.24.103:9999" + "/auth/oauth2/token");
        ObjectMapper objectMapper = new ObjectMapper();
        BookExposureEntity bookExposureEntity = new BookExposureEntity();
        bookExposureEntity.setUsername("hndzyx");
        bookExposureEntity.setPassword("123456");
        bookExposureEntity.setFromSys("1");
        bookExposureEntity.setProductId("1850710022997258241");
        bookExposureEntity.setResourceId(byId1.getResourceId() + "");
        String json = null;
        try {
            json = objectMapper.writeValueAsString(bookExposureEntity);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        String jsonString =
                //"{\"code\":0,\"msg\":\"汉魏士人的思想历程详情\",\"data\":{\"id\":\"1724497002389471233\",\"bookName\":\"汉魏士人的思想历程\",\"author\":\"孙生 白笑天\",\"bookNumber\":\"978-7-227-04912-8\",\"briefIntroduction\":null,\"press\":\"宁夏人民出版社\",\"publicationTime\":\"2011-12-01\",\"bookClassification\":\"社会科学总论\",\"dataTypeId\":\"1719628518976913409\",\"bookDataType\":\"电子书\",\"bookGradeType\":\"B\",\"labelType\":null,\"copyrightOwner\":\"北京九州文心\",\"file\":[{\"fileName\":\"汉魏士人的思想历程\",\"size\":3.0,\"resourceName\":\"汉魏士人的思想历程\",\"fileUrl\":\"https://zhongtai.zhongjiaoyuntu.com:9002/dzyx/20240428/黄老师上传1宁夏吉林/汉魏士人的思想历程/汉魏士人的思想历程.pdf\",\"type\":\"pdf\"}],\"coverPictures\":[{\"coverPictureName\":\"汉魏士人的思想历程\",\"coverPictureUrl\":\"https://zhongtai.zhongjiaoyuntu.com:9002/dzyx/20240428/黄老师上传1宁夏吉林/汉魏士人的思想历程/汉魏士人的思想历程.jpg\",\"type\":\"jpg\"}]}}\n";
                resourceType(token, "http://10.100.24.103:9999" + "/digitallibrary/bookExposure/getProductResourcesDetails", json);
        log.info("资源返回报文=============={}", jsonString);
        Gson gson = new Gson();
        AudioDetailVo audioDetailVo = gson.fromJson(jsonString, AudioDetailVo.class);
        audioDetailVo.getData().setBookCode("https://dangjian.shuzinongyue.com:8946/audio?id=" + byId1.getId());
        return audioDetailVo;
    }

}
